package com.jjx.trainingapi.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.util.StrUtil;
import com.jjx.trainingapi.common.RT;
import com.jjx.trainingapi.entity.po.DcCourse;
import com.jjx.trainingapi.entity.po.QuestionItem;
import com.jjx.trainingapi.entity.po.UserRecordError;
import com.jjx.trainingapi.entity.po.UserRecordFavorite;
import com.jjx.trainingapi.entity.vo.DoAnswerVo;
import com.jjx.trainingapi.service.QuestionItemService;
import com.jjx.trainingapi.service.UserRecordErrorService;
import com.jjx.trainingapi.service.UserRecordFavoriteService;
import com.jjx.trainingapi.util.LoginManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 答题控制层
 *
 * @author LoveHuahua
 * @date 2022年05月23日 18:49
 * @description believe in yourself
 */
@RestController
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    QuestionItemService questionItemService;

    @Autowired
    UserRecordErrorService userRecordErrorService;

    @Autowired
    UserRecordFavoriteService userRecordFavoriteService;

    /**
     * 开始答题
     *
     * @param questionItem
     * @return
     */
    @PostMapping("/doAnswer")
    @SaCheckLogin
    public RT<DoAnswerVo> doAnswer(@RequestBody QuestionItem questionItem) {
        return RT.ok(questionItemService.doAnswer(questionItem));
    }


    /**
     * 题目列表
     *
     * @param questionItem
     * @return
     */
    @PostMapping("/questionList")
    @SaCheckLogin
    public RT<List<QuestionItem>> questionList(@RequestBody QuestionItem questionItem) {
        if (questionItem.getType() != null && questionItem.getType() == 0) {
            questionItem.setType(null);
        }
        List<QuestionItem> list = questionItemService.lambdaQuery()
                .eq(questionItem.getType() != null && questionItem.getType() != 0, QuestionItem::getType, questionItem.getType())
                .eq(questionItem.getSectionId() != null ,QuestionItem::getSectionId, questionItem.getSectionId())
                .like(StrUtil.isNotBlank(questionItem.getQuestion()), QuestionItem::getQuestion, questionItem.getQuestion())
                .orderByAsc(QuestionItem::getId)
                .list();
        return RT.ok(list);
    }


    /**
     * 添加收藏
     *
     * @param param {questionId:题目id}
     * @return
     */
    @PostMapping("/addFavoriteQuestion")
    @SaCheckLogin
    public RT<String> addFavoriteQuestion(@RequestBody UserRecordError param) {
        userRecordFavoriteService.addFavoriteQuestion(param);
        return RT.ok();
    }


    /**
     * 收藏列表
     *
     * @param param {questionId:题目id}
     * @return
     */
    @PostMapping("/favoriteQuestionList")
    @SaCheckLogin
    public RT<List<QuestionItem>> favoriteQuestionList(@RequestBody DcCourse param) {
        List<QuestionItem> data = userRecordFavoriteService.favoriteQuestionList(param);
        return RT.ok(data);
    }


    /**
     * 检测是否收藏
     *
     * @param param {questionId:题目id}
     * @return
     */

    @PostMapping("/checkFavoriteQuestion")
    @SaCheckLogin
    public RT<Boolean> checkFavoriteQuestion(@RequestBody UserRecordError param) {
        boolean isFavorite = userRecordFavoriteService.checkFavoriteQuestion(param);
        return RT.ok(isFavorite);
    }


    /**
     * 取消收藏
     *
     * @param param {questionId:题目id}
     * @return
     */
    @PostMapping("/removeFavoriteQuestion")
    @SaCheckLogin
    public RT<String> removeFavoriteQuestion(@RequestBody UserRecordError param) {
        //删除收藏
        userRecordFavoriteService.lambdaUpdate().eq(UserRecordFavorite::getQuestionItemId, param.getQuestionItemId()).eq(UserRecordFavorite::getUserId, LoginManager.getUserId()).remove();
        return RT.ok();
    }


    /**
     * 获取错题
     *
     * @return
     */
    @PostMapping("/getErrorQuestion")
    @SaCheckLogin
    public RT<List<QuestionItem>> getErrorQuestion(@RequestBody DcCourse course) {
        return RT.ok(questionItemService.getErrorQuestion(course));
    }


}
